package com.elf.reactnative.nativepackage;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.elf.reactnative.ReactActivity;
import com.elf.reactnative.ReactFragmentActivity;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import javax.annotation.Nonnull;

/**
 * 打开新的activity
 * Created by Lidong on 2020/2/17.
 */
public class NavigationModule extends ReactContextBaseJavaModule {

    private final static String ARG_EXTRAS = "extras";
    private final static int REQUEST_CODE = 0;

    private final static String CODE_ERROR = "error";
    private final static String CODE_CANCEL = "cancel";
    private final static String CODE_OPEN_FAILURE = "open failure";
    private final static String MSG_ACTIVITY_NOT_EXIST = "窗口不存在";
    private final static String MSG_CANCEL = "操作已取消";
    private final static String MSG_OPEN_FAILURE = "打开失败";

    private Promise mPromise;

    public NavigationModule(@Nonnull ReactApplicationContext reactContext) {
        super(reactContext);
        reactContext.addActivityEventListener(new BaseActivityEventListener() {
            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
                if (mPromise == null) {
                    return;
                }
                if (resultCode == Activity.RESULT_OK) {
                    if (data != null && data.hasExtra("data")) {
                        mPromise.resolve(data.getStringExtra("data"));
                    } else {
                        mPromise.resolve("");
                    }
                } else {
                    mPromise.reject(CODE_CANCEL, MSG_CANCEL);
                }
                mPromise = null;
            }
        });
    }

    @Nonnull
    @Override
    public String getName() {
        return "Navigation";
    }

    @ReactMethod
    public void openReactActivity(String jsBundleInfo, String extras, Promise promise) {
        try {
            Activity activity = getCurrentActivity();
            if (activity == null) {
                promise.reject(CODE_ERROR, MSG_ACTIVITY_NOT_EXIST);
                return;
            }
            mPromise = promise;

            ReactActivity.open(getCurrentActivity(), jsBundleInfo, extras, REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            promise.reject(CODE_OPEN_FAILURE, MSG_OPEN_FAILURE);
        }
    }

    @ReactMethod
    public void openReactFragmentActivity(String jsBundleInfo, String extras, Promise promise) {
        try {
            Activity activity = getCurrentActivity();
            if (activity == null) {
                promise.reject(CODE_ERROR, MSG_ACTIVITY_NOT_EXIST);
                return;
            }
            mPromise = promise;

            ReactFragmentActivity.open(getCurrentActivity(), jsBundleInfo, extras, REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            promise.reject(CODE_OPEN_FAILURE, MSG_OPEN_FAILURE);
        }
    }

    @ReactMethod
    public void openActivity(String className, String extras, Promise promise) {
        try {
            Class activityClass = Class.forName(className);
            Intent intent = new Intent(getCurrentActivity(), activityClass);
            if (!TextUtils.isEmpty(extras)) {
                intent.putExtra(ARG_EXTRAS, extras);
            }
            startActivity(intent, promise);
        } catch (Exception e) {
            e.printStackTrace();
            promise.reject(CODE_OPEN_FAILURE, MSG_OPEN_FAILURE);
        }
    }

    @ReactMethod
    public void runAction(String action, String extras, Promise promise) {
        try {
            Intent intent = new Intent(action);
            if (!TextUtils.isEmpty(extras)) {
                intent.putExtra(ARG_EXTRAS, extras);
            }
            startActivity(intent, promise);
        } catch (Exception e) {
            e.printStackTrace();
            promise.reject(CODE_OPEN_FAILURE, MSG_OPEN_FAILURE);
        }
    }

    @ReactMethod
    public void runScheme(String scheme, String extras, Promise promise) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(scheme));
            if (!TextUtils.isEmpty(extras)) {
                intent.putExtra(ARG_EXTRAS, extras);
            }
            startActivity(intent, promise);
        } catch (Exception e) {
            e.printStackTrace();
            promise.reject(CODE_OPEN_FAILURE, MSG_OPEN_FAILURE);
        }
    }

    private void startActivity(Intent intent, Promise promise) {
        Activity activity = getCurrentActivity();
        if (activity == null) {
            promise.reject(CODE_ERROR, MSG_ACTIVITY_NOT_EXIST);
            return;
        }
        mPromise = promise;
        activity.startActivityForResult(intent, REQUEST_CODE);
    }
}
